using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data;
using System.Data.Entity.Infrastructure;
using ShopPowerTemplate.Services.Properties;
using ShopPowerTemplate.Entities;

namespace ShopPowerTemplate.Services
{
    public class UserServiceBase : Service
    {
		public UserServiceBase(IEntitiesDB entitiesDB) : base(entitiesDB) { }
		
        public virtual Int32 Add(User user)
        {
            EntitiesDB.UserSet.Add(user);
            EntitiesDB.SaveChanges();
            return user.Id;
        }

        public virtual void Change(User user)
        {
		    EntitiesDB.Entry(user).State = EntityState.Modified;

            EntitiesDB.SaveChanges();
        }
		
		public virtual void Remove(int id)
        {
			User user = Find(id);
            try
            {
                EntitiesDB.UserSet.Remove(user);
                EntitiesDB.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw new RemoveException(string.Format(Resources.RemoveException, user.ToString()));
            }
        }

        public virtual void Remove(User user)
        {
			Remove(user.Id);
        }        

        public virtual User Find(int id)
        {
			User user = 
				EntitiesDB.UserSet
					.FirstOrDefault(x => x.Id == id);
			return user;
        }

        public virtual List<User> FindAll()
        {
            return EntitiesDB.UserSet.OrderBy(x => x.Password).ToList();
        }
		
		public virtual List<User> Search(string searchValue)
        {
            return Search(searchValue, string.Empty, true, 0, 0);
        }

        public virtual List<User> Search(string searchValue, int pageIndex, int pageSize)
        {
            return Search(searchValue, string.Empty, true, pageIndex, pageSize);
        }

        public virtual List<User> Search(string searchValue, string orderBy)
        {
            return Search(searchValue, orderBy, true, 0, 0);
        }

        public virtual List<User> Search(string searchValue, string orderBy, int pageIndex, int pageSize)
        {
            return Search(searchValue, orderBy, true, pageIndex, pageSize);
        }

        public virtual List<User> Search(string searchValue, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize = 10)
        {
            IQueryable<User> query = EntitiesDB.UserSet.Where(x => string.IsNullOrEmpty(searchValue) || x.Login.Contains(searchValue)
				|| x.Password.Contains(searchValue));
			
			if(orderBy == string.Empty)
				orderBy = "Id";			
			
			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }
		
		public virtual List<User> Search(Expression<Func<User, bool>> searchCriteria, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize=10)
        {
            IQueryable<User> query = EntitiesDB.UserSet.Where(searchCriteria);
            
			if(orderBy == string.Empty)
				orderBy = "Id";			
			
			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public virtual int SearchCount(string searchValue)
        {
            return EntitiesDB.UserSet.Where(x => string.IsNullOrEmpty(searchValue) || x.Login.Contains(searchValue)
				|| x.Password.Contains(searchValue)).Count();
        }
		
		public virtual List<User> FindAvailableUserList<TKey>(List<User> notAvailableList, string searchValue, System.Linq.Expressions.Expression<Func<User, TKey>> orderBy, bool ascending, int pageIndex, int pageSize)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            IQueryable<User> query = EntitiesDB.UserSet.Where(a => !notAvailableIDList.Any(id => id == a.Id)).Where(x => string.IsNullOrEmpty(searchValue) || x.Login.Contains(searchValue)
				|| x.Password.Contains(searchValue));

			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public virtual int FindAvailableUserListCount(List<User> notAvailableList, string searchValue)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            return EntitiesDB.UserSet.Where(a => !notAvailableIDList.Any(id => id == a.Id)).Where(x => string.IsNullOrEmpty(searchValue) || x.Login.Contains(searchValue)
				|| x.Password.Contains(searchValue)).Count();
        }
	
    }
}
